/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.common;

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;

/**
 *
 * @author sorasaks
 */
public class MessageResources {

    private static Logger LOG = Logger.getLogger(MessageResources.class);
    protected static String bundle = "Resources";

    public static String getLabel(String key) throws UnsupportedEncodingException {
        return getLabel(key, (Locale) null);
    }

    public static String getLabel(String key, Object[] obj) throws UnsupportedEncodingException {
        return getLabel(key, obj, (Locale) null);
    }

    public static String getLabel(String key, Locale locale) throws UnsupportedEncodingException {
        ResourceBundle resources = null;
        String s = null;
        try {
            if (locale != null) {
                //  LOG.info("Locale 111111 >>>>>>>>>>>>>>>>>>>>> ="+locale.toString());
                resources = ResourceBundle.getBundle(bundle, locale);
                s = resources.getString(key);
            } else {
                //     LOG.info("Locale 222222 >>>>>>>>>>>>>>>>>>>>> ="+Locale.getDefault().toString());
                s = getLabel(key, Locale.getDefault());
            }
        } catch (Exception e) {
            resources = ResourceBundle.getBundle(bundle, Locale.getDefault());
            try {
                s = resources.getString(key);
            } catch (Exception e1) {
                s = "";
            }
        }

        return s != null ? s : "";
    }

    public static String getLabel(String key, String defaultValue) {
        String s = null;
        try {
            s = getLabel(key);
        } catch (Exception e) {
        }
        return (s != null && !s.equals(key)) ? s : defaultValue;
    }

    public static String getLabel(String key, Object[] obj, Locale locale) throws UnsupportedEncodingException {
        String s = getLabel(key, locale);
        return (s != null && !s.equals("")) ? MessageFormat.format(s, obj) : "";
    }
}
